
/**
 * @author	胡涛
 *@version 创建时间：2011-12-14 上午10:56:24

 * 
 */
package com.jspgou.cms.action.admin.main;

import static com.jspgou.common.page.SimplePage.cpn;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.CookieUtils;
import com.jspgou.cms.entity.Product;
import com.jspgou.cms.entity.ProductFashion;
import com.jspgou.cms.entity.ProductType;
import com.jspgou.cms.manager.OrderItemMng;
import com.jspgou.cms.manager.ProductFashionMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.manager.ProductTypeMng;

@Controller
public class ProductStatisticsAct {
	private static final Logger log = LoggerFactory.getLogger(ProductStatisticsAct.class);
	
	@RequestMapping("/productStatistics/v_productLack.do")
	public String productLack(Integer status,Integer count,Integer pageNo,HttpServletRequest request,ModelMap model){
		Pagination pagination = productFashionMng.productLack(status,count, cpn(pageNo), CookieUtils
				.getPageSize(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("count", count);
		model.addAttribute("status", status);
		return "sale/productLack";
	}
	@RequestMapping("/productStatistics/o_updateRemind.do")
	public String updateRemind(Integer status,Integer count,Integer pageNo,Long fashionid,HttpServletRequest request,ModelMap model){
		ProductFashion fashion = productFashionMng.findById(fashionid);
		fashion.setLackRemind(status);
		productFashionMng.update(fashion);
		model.addAttribute("status", status==1?0:1);
		model.addAttribute("count", count);
		return "redirect:v_productLack.do";
	}
	
	@RequestMapping("/productStatistics/v_productSaleTop.do")
	public String productSaleTop(Integer count,Integer pageNo,HttpServletRequest request,ModelMap model){
		Pagination pagination = productFashionMng.getSaleTopPage(cpn(pageNo), CookieUtils
				.getPageSize(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pageNo);
		return "sale/productSaleTop";
	}
	
	@RequestMapping("/productStatistics/v_productProfitTop.do")
	public String productProfitTop(Long ctgid,Long typeid,Integer pageNo,Integer pageSize,
			HttpServletRequest request,ModelMap model){
//		if(pageSize==null){
//			pageSize=20;
//		}
		List<ProductType> types = productTypeMng.getList(new Long(1));
		pageSize = CookieUtils.getPageSize(request);
		List<Object[]> list = orderItemMng.profitTop(ctgid,typeid,cpn(pageNo), pageSize);
		List<Object[]> result = new ArrayList<Object[]>();
		for(int i = 0;i<list.size();i++){
			long productid = Long.parseLong(list.get(i)[0].toString());
			Integer count = Integer.parseInt(list.get(i)[1].toString());
			BigDecimal deci=new BigDecimal(list.get(i)[2].toString());
			Float profit = Float.parseFloat(list.get(i)[2].toString());
			Product product = productMng.findById(productid);
			Object[] obj = new Object[]{product,count,deci};
			result.add(obj);
		}
		Integer totalCount = orderItemMng.totalCount(null,typeid);
		Integer totalPage = totalCount%pageSize==0 ? totalCount/pageSize:totalCount/pageSize+1;
		model.addAttribute("types", types);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("pageNo", cpn(pageNo));
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("list", result);
		model.addAttribute("typeid", typeid);
		
		return "sale/productProfitTop";
	}
	
	@Autowired
	private ProductFashionMng productFashionMng;
	@Autowired
	private OrderItemMng orderItemMng;
	@Autowired
	private ProductMng productMng;
	@Autowired
	private ProductTypeMng productTypeMng;
	
}

